<?php
// app/Http/Requests/ProgrammingLanguageRequest.php
namespace App\Http\Requests;

use App\Models\Chapter;
use App\Models\ProgrammingLanguage;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;


class ChapterRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch ($this->method()) {
            case "POST":
                return [
                    'chapter_name' => ['required', 'string', Rule::unique(Chapter::getTableName())], //章节名
                    'programming_language_id' => ['required', 'integer', Rule::exists(ProgrammingLanguage::getTableName(), 'id')], //编程语言名称id
                ];
            case "PUT":
            case "PATCH":
                return [
                    'chapter_name' => ['nullable', 'string', Rule::unique(Chapter::getTableName())], //章节名
                    'programming_language_id' => ['nullable', 'integer', Rule::exists(ProgrammingLanguage::getTableName(), 'id')], //编程语言名称id
                ];
        }
        return [];
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 获取验证错误的自定义属性
     *
     * @return array
     */
    public function attributes()
    {
        // 替换 :attribute 占位符内容
        return [
            'chapter_name' => '章节名',
            'programming_language_id' => '编程语言名称id'
        ];
    }
}
